package server;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import java.awt.GridLayout;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.JTextArea;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
/**
 * Een GUI voor een server van PentagoXL
 * kan een poort selecteren en toont berichten van de server
 * @author Tim en Stef
 *
 */
public class ServerGUI extends JFrame implements Observer{

	private JPanel contentPane;
	private JTextField textField_Port;
	private JTextArea textArea_msg;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ServerGUI frame = new ServerGUI();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public ServerGUI() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		
		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);
		
		JMenu mnConnection = new JMenu("Connection");
		menuBar.add(mnConnection);
		
		JPanel panel = new JPanel();
		mnConnection.add(panel);
		panel.setLayout(new GridLayout(1, 0, 0, 0));
		
		JPanel panel_2 = new JPanel();
		panel.add(panel_2);
		
		JLabel label_1 = new JLabel("Port:");
		panel_2.add(label_1);
		
		textField_Port = new JTextField();
		textField_Port.setText("8080");
		textField_Port.setColumns(10);
		panel_2.add(textField_Port);
		
		JMenuItem mntmStartListening = new JMenuItem("Start Listening");
		mntmStartListening.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				startServer();
			}
		});
		mnConnection.add(mntmStartListening);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		
		JScrollPane scrollPane = new JScrollPane();
		contentPane.add(scrollPane, BorderLayout.CENTER);
		
		textArea_msg = new JTextArea();
		scrollPane.setViewportView(textArea_msg);
	}

	@Override
	/**
	 * Handelt berichten af van het server object en toont deze in de GUI
	 */
	public void update(Observable o, Object arg) {
		if(o instanceof Server){
			if(arg!=null&&arg instanceof String){
				appendMessage((String) arg);
			}
		}
	}
	private void appendMessage(String s){
		this.textArea_msg.append(s+'\n');
		
	}
	private void startServer(){
		int port; 
		try {
            port = Integer.parseInt(this.textField_Port.getText());
            Server s = new Server(port);
            s.addObserver(this);
            new Thread(s).start();
		} catch (NumberFormatException e) {
            appendMessage("ERROR: geen geldig portnummer!");
            return;
        }	
	}
}
